package com.itheima.service.serviceimpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemAndGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckItemAndGroupMapper checkItemAndGroupMapper;

    @Override
    public void add(Integer[] checkitemIds, CheckGroup checkGroup) {
        //添加检查组基本信息
        checkGroupMapper.add(checkGroup);
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                //添加检查组和检查项的关联关系
                checkItemAndGroupMapper.add(checkGroup.getId(), checkitemId);
            }
        }


    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckGroup> page = (Page<CheckGroup>) checkGroupMapper.selectByCondition(queryPageBean);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void delete(Integer id) {
        checkGroupMapper.delete(id);
    }

    @Override
    public void edit(CheckGroup checkGroup) {
        checkGroupMapper.edit(checkGroup);
    }

    @Override
    public CheckGroup findById(Integer id) {

        return checkGroupMapper.findById(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupMapper.findAll();
    }

}
